/**
 * Created by Lukas Krupa (lukas0krupa@gmail.com).
 */
package sk.lukaskrupa.guess.the.color.listeners;

import sk.lukaskrupa.guess.the.color.logging.Logging;
import sk.lukaskrupa.guess.the.color.ui.Help;
import sk.lukaskrupa.guess.the.color.ui.R;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Listens to buttons on Help activity.
 * 
 * @author Lukas Krupa (lukas0krupa@gmail.com)
 * 
 */
public class HelpButtonListener implements OnClickListener {

	private static final Logging logger = Logging.getLog(HelpButtonListener.class);

	private final int[] imageIds = new int[]{ //
			R.drawable.helpwelcome,//
			R.drawable.helpmenu1, R.drawable.helpmenu2, R.drawable.helpmenu3,
			R.drawable.helpmenu4, //
			R.drawable.helpsettings1, R.drawable.helpsettings2, R.drawable.helpsettings3, R.drawable.helpsettings4,
			R.drawable.helpsettings5,//
			R.drawable.helpgame1, R.drawable.helpgame2, R.drawable.helpgame3 //

	};
	private final int[] textIds = new int[]{//
	R.string.helpwelcome,//
			R.string.helpmenu1, R.string.helpmenu2, R.string.helpmenu3, R.string.helpmenu4, //
			R.string.helpsettings1, R.string.helpsettings2, R.string.helpsettings3, R.string.helpsettings4, R.string.helpsettings5,//
			R.string.helpgame1, R.string.helpgame2, R.string.helpgame3 //

	};

	private final int itemUpperBound = this.imageIds.length - 1;
	private final int itemBottomBound = 0;
	private int itemOn = this.itemBottomBound;

	private final Help help;

	/**
	 * Constructs listener and save reference to Help activity.
	 * 
	 * @param help
	 *            to be used for closeing
	 */
	public HelpButtonListener(Help help) {
		this.help = help;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onClick(View v) {
		logger.debug("onClick", "You clicked on button with ID=" + v.getId());
		if (v.getId() == R.id.backHelp) {

			logger.debug("onClick", "Back to previous help item.");
			backHelpItem();
		} else if (v.getId() == R.id.nextHelp) {

			logger.debug("onClick", "To next help item.");
			nextHelpItem();
		} else if (v.getId() == R.id.exitHelp) {

			logger.debug("onClick", "Exiting help.");
			this.help.finish();
		}
	}

	/**
	 * Navigates to next help page.
	 */
	private void nextHelpItem() {

		if (this.itemOn >= this.itemUpperBound) {
			logger.debug("nextHelpItem", "Navigating to next help page: current=" + this.itemOn + ", to=" + (this.itemOn + 1) + ", pages="
					+ this.itemUpperBound);
		}

		// Setting new page
		this.itemOn += 1;
		((ImageView) this.help.findViewById(R.id.contentImage)).setImageDrawable(this.help.getResources().getDrawable(
				this.imageIds[this.itemOn]));
		((TextView) this.help.findViewById(R.id.contentText)).setText(this.help.getResources().getString(this.textIds[this.itemOn]));

		// Enabling NEXT
		this.help.findViewById(R.id.backHelp).setEnabled(true);

		// Disabling BACK in case is on bound
		if (this.itemOn == this.itemUpperBound) {
			this.help.findViewById(R.id.nextHelp).setEnabled(false);
		}
	}

	/**
	 * Navigates to previous help page.
	 */
	private void backHelpItem() {

		logger.debug("backHelpItem", "Navigating back to previous help page: current=" + this.itemOn + ", to=" + (this.itemOn - 1)
				+ ", pages=" + this.itemUpperBound);

		// Setting new page
		this.itemOn -= 1;
		((ImageView) this.help.findViewById(R.id.contentImage)).setImageDrawable(this.help.getResources().getDrawable(
				this.imageIds[this.itemOn]));
		((TextView) this.help.findViewById(R.id.contentText)).setText(this.help.getResources().getString(this.textIds[this.itemOn]));

		// Enabling NEXT
		this.help.findViewById(R.id.nextHelp).setEnabled(true);

		// Disabling BACK in case is on bound
		if (this.itemOn == this.itemBottomBound) {
			this.help.findViewById(R.id.backHelp).setEnabled(false);
		}

	}
}
